package com.hn658.groupshare.security.dao.impl;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.hn658.framework.dataaccess.redis.AbstractRedisDAO;
import com.hn658.framework.shared.entity.BaseEntity;
import com.hn658.groupshare.common.enums.VerifyCodeType;
import com.hn658.groupshare.common.utils.RedisKeyHelper;
import com.hn658.groupshare.security.dao.IVerifyCodeRedisDao;

@Service
public class VerifyCodeRedisDaoImpl extends AbstractRedisDAO<BaseEntity> implements IVerifyCodeRedisDao {

	@Autowired
	@Qualifier("redisTemplate")
	@Override
	public void setTemplate(StringRedisTemplate template) {
		super.template = template;
	}

	@Override
	public void saveVerifyCode(String phone, VerifyCodeType type, String code) {
		String key = RedisKeyHelper.mobileVerifyCode(phone, type);
		valueOps.set(key, code);
		// 设置过期时间为30分钟
		this.template.expire(key, 30, TimeUnit.MINUTES);
	}

	@Override
	public void delectVerifyCode(String phone, VerifyCodeType type) {
		String key = RedisKeyHelper.mobileVerifyCode(phone, type);
		this.template.delete(key);
	}

	@Override
	public String getVerifyCode(String phone, VerifyCodeType type) {
		String key = RedisKeyHelper.mobileVerifyCode(phone, type);
		return valueOps.get(key);
	}

	@Override
	public void saveVerifyCode(String phone, VerifyCodeType type, String code,
			Integer minutes) {
		String key = RedisKeyHelper.mobileVerifyCode(phone, type);
		valueOps.set(key, code);
		// 设置过期时间为30分钟
		this.template.expire(key, minutes, TimeUnit.MINUTES);
	}
	

}
